﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core.ServiceModel;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.ZNH.ZMCommon.Validators;
using Telerik.Windows.Controls;
using Baosight.ZNH.ZMCommon.Prompt;

namespace Baosight.ZNH.ZM.UI
{
    public partial class ZM80 : EFViewCmes
    {
        public ZM80()
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
        }

        public override void Initialize(EIInfo initData)
        {
            base.Initialize(initData);
        }

        /// <summary>
        /// 初始化父类EFViewCmesProperty的值，每次点击“确定”、“返回”时执行此方法，从新赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.DataFields = this.spWindows.GetChildObjects<EFDataField>(null, true);
            base.ViewProperty = _view;
        }

        private void CommitClick(object sender, RoutedEventArgs e)
        {
            if (this.new_password.Password == null
                    || this.new_password.Password.Equals(""))
            {
                ValidateMessagePopup("新密码未输入！");
                return;
            }

            if (this.cnew_password.Password == null
                    || this.cnew_password.Password.Equals(""))
            {
                ValidateMessagePopup("新密码确认未输入！");
                return;
            }

            if (validateNewPassWord())
            {
                EIInfo _info = new EIInfo();
                _info.Set("service", "ZM80");
                _info.Set("method", "update");
                _info.Set("fdNewWord", new_password.Password);
                this.Submit(_info, submitCompleted);
            }
            else
            {
                ValidateMessagePopup("新密码和确认密码不一致，请重新输入！");
                return;
            }
        }

        public void submitCompleted(object sender, DoWorkCompletedEventArgs args)
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.Close();
        }

        private void CancelClick(object sender, RoutedEventArgs e)
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.Close();
        }

        /// <summary>
        /// 验证新密码与新密码确认是否相同
        /// </summary>
        /// <returns></returns>
        private Boolean validateNewPassWord()
        {
            if (this.new_password.Password != null
                    && !this.new_password.Password.Equals("")
                    && this.new_password.Password.Equals(this.cnew_password.Password))
            {
                return true;
            }
            else
                return false;
        }

        /// <summary>
        /// 验证失败，消息提示
        /// </summary>
        public void ValidateMessagePopup(string msg)
        {
            Prompts pop = Prompts.getInstance();
            pop.SetMsg(msg);
            pop.Show(6);

            return;
        }
    }
}
